package com.lagou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.lagou.entity.Result;
import com.lagou.goods.pojo.Sku;
import com.lagou.goods.pojo.Spu;
import com.lagou.page.feign.CategoryFeign;
import com.lagou.page.feign.SkuFeign;
import com.lagou.page.feign.SpuFeign;
import com.lagou.page.service.PageService;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Fxz
 * @version 1.0
 * @date 2021-10-07 15:45
 */
@Service
@RequiredArgsConstructor
public class PageServiceImpl implements PageService {

    private final SpuFeign spuFeign;

    private final SkuFeign skuFeign;

    private final CategoryFeign categoryFeign;

    private final TemplateEngine templateEngine;

    @Value("${pagepath}")
    private String pagePath;

    /**
     * 根据spuId生成静态页
     *
     * @param id
     */
    @SneakyThrows
    @Override
    public void createHtml(String id) {
        //存储所有生成静态页所需要的数据
        Map<String, Object> resultMap = new HashMap<>();
        //加载spu对象 分类 sku列表
        loadGoodsInfo(id, resultMap);
        //在指定位置通过模板引擎生成静态页
        createStaticPage(id, resultMap);
    }

    private void createStaticPage(String id, Map<String, Object> resultMap) throws FileNotFoundException {
        Context context = new Context();
        //在模板中可以通过map的key获取value的值
        context.setVariables(resultMap);
        //设置输出目录
        File file = new File(pagePath);
        if (!file.exists()) {
            file.mkdir();
        }
        //通过模板引擎对象生成静态页
        PrintWriter printWriter = new PrintWriter(file + "/" + id + ".html");
        templateEngine.process("item", context, printWriter);
        printWriter.close();
    }

    private void loadGoodsInfo(String id, Map<String, Object> resultMap) {
        //加载spu对象
        Result result = spuFeign.findById(id);
        Spu spu = JSON.parseObject(JSON.toJSONString(result.getData()), Spu.class);
        resultMap.put("spu", spu);
        String images = spu.getImages();
        List<Map> maps = JSON.parseArray(images, Map.class);
        List<String> imagesList = new LinkedList<>();
        if (maps != null && maps.size() > 0) {
            for (Map map : maps) {
                imagesList.add(String.valueOf(map.get("url")));
            }
            resultMap.put("imageList", imagesList);
        }
        //获得分类
        resultMap.put("category1", categoryFeign.findById(spu.getCategory1Id()).getData());
        resultMap.put("category2", categoryFeign.findById(spu.getCategory2Id()).getData());
        resultMap.put("category3", categoryFeign.findById(spu.getCategory3Id()).getData());
        //根据spuId获取sku集合
        List<Sku> skuList = skuFeign.findListBySpuId(id);
        resultMap.put("skuList", skuList);
        //获取规格
        String specItems = spu.getSpecItems();
        Map map = JSON.parseObject(specItems, Map.class);
        resultMap.put("specificationList", map);
    }
}
